package com.zxy.ziems.collector.progress;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Task;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.Background;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;

import java.util.Objects;

/**
 * Loading加载页面
 * @author 10348359
 * @create 2024-04-22 22:41
 */
public class ProgressHUD {

    private Stage stage;
    private Task<?> work;

    private ProgressHUD() {
    }

    /**
     * 创建
     *
     * @param parent
     * @param work
     * @param ad
     * @return
     */
    public static ProgressHUD of(Stage parent, Task<?> work, String ad) {
        ProgressHUD ps = new ProgressHUD();
        ps.work = Objects.requireNonNull(work);
        ps.initUI(parent, ad);
        return ps;
    }

    /**
     * 显示
     */
    public void show() {
        new Thread(work).start();
        stage.show();
    }


    /**
     * 关闭
     */
    public void close() {
        stage.close();
    }


    private void initUI(Stage parent, String ad) {
        stage = new Stage();
        stage.initOwner(parent);
        // style
        stage.initStyle(StageStyle.UNDECORATED);
        stage.initStyle(StageStyle.TRANSPARENT);
        stage.initModality(Modality.APPLICATION_MODAL);

        // message
        Label adLbl = new Label(ad);
        adLbl.setTextFill(Color.BLUE);

        // progress
        ProgressIndicator indicator = new ProgressIndicator();
        indicator.setProgress(-1);
        indicator.progressProperty().bind(work.progressProperty());

        // pack
        VBox vBox = new VBox();
        vBox.setSpacing(10);
        vBox.setBackground(Background.EMPTY);
        vBox.getChildren().addAll(indicator, adLbl);

        // scene
        Scene scene = new Scene(vBox);
        scene.setFill(null);
        stage.setScene(scene);
        stage.setWidth(ad.length() * adLbl.getFont().getSize() + 10);
        stage.setHeight(100);
        parent.widthProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observableValue, Number number, Number t1) {
                double x = parent.getX() + (parent.getWidth() - stage.getWidth()) / 2;
                stage.setX(x);
            }
        });

        parent.heightProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observableValue, Number number, Number t1) {
                double y = parent.getY() + (parent.getHeight() - stage.getHeight()) / 2;
                stage.setY(y);
            }
        });

        // show center of parent


        // close if work finish
        work.setOnFailed((e)->stage.close());
        work.setOnSucceeded(e -> stage.close());
    }
}
